﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace D3crft.Windows.UserControls {
    /// <summary>
    /// Interaktionslogik für ucButton_CloseWindow.xaml
    /// </summary>
    public partial class ucButton_CloseWindow : UserControl {

        public event EventHandler evMouseUp;

        int iMode = -1;
        private double dOpacity = 0.0;

        DispatcherTimer oTimer;

        public ucButton_CloseWindow() {
            InitializeComponent();

            oTimer = new DispatcherTimer();

            oTimer.Interval = TimeSpan.FromMilliseconds(40.0);
            oTimer.Tick += new EventHandler(ev_oTimer_Tick);
        }

        private void ev_Image_MouseEnter(object sender, MouseEventArgs ea) {
            do_FadeIn();
            Image.Source = new BitmapImage(new Uri("pack://application:,,,/D3Crft;component/images/icon_window_close_white.png", UriKind.Absolute));
        }

        private void ev_Image_MouseLeave(object sender, MouseEventArgs ea) {
            do_FadeOut();
            Image.Source = new BitmapImage(new Uri("pack://application:,,,/D3Crft;component/images/icon_window_close_lightblue.png", UriKind.Absolute));
        }

        private void ev_Image_MouseUp(object sender, MouseEventArgs ea) {
            if (evMouseUp != null) evMouseUp(this, EventArgs.Empty);
        }

        private void ev_oTimer_Tick(object sender, EventArgs e) {
            if (iMode == 0) {
                if (dOpacity >= 1.0) {
                    oTimer.Stop();
                    iMode = -1;
                    dOpacity = 1.0;
                }
                else {
                    dOpacity += 0.2;
                }
                Effect.Opacity = dOpacity;
            }
            else if (iMode == 1) {
                if (dOpacity <= 0.0) {
                    oTimer.Stop();
                    iMode = -1;
                    dOpacity = 0.0;
                }
                else {
                    dOpacity -= 0.2;
                }
                Effect.Opacity = dOpacity;
            }
            else {
                oTimer.Stop();
            }
        }


        private void do_FadeIn() {
            iMode = 0;
            oTimer.Start();
        }

        private void do_FadeOut() {
            iMode = 1;
            oTimer.Start();
        }
    }
}
